library(tidyverse)
df <- haven::read_dta("~\\Replication\\data_collapsed.dta") %>% as.data.frame()

df$qual_text <- ""
df$qual_text[df$qual == 1] <- "No failure"
df$qual_text[df$qual == 2] <- "Failure\nw/ explanation"
df$qual_text[df$qual == 3] <- "Failure\nw/o explanation"
df$qual_text <- factor(df$qual_text, levels = c("No failure",
                                            "Failure\nw/ explanation",
                                            "Failure\nw/o explanation"))

df$com_text <- ""
df$com_text[df$com == 1] <- "App"
df$com_text[df$com == 2] <- "SST"
df$com_text[df$com == 3] <- "Face-to-face"
df$com_text <- factor(df$com_text, levels = c( "Face-to-face", "SST","App"))

df$act_text <- ""
df$act_text[df$act == 1] <- "Passport"
df$act_text[df$act == 2] <- "Certificate of\ngood conduct"
df$act_text[df$act == 3] <- "Social housing"
df$act_text <- factor(df$act_text, 
                      levels = c("Passport", 
                                 "Certificate of\ngood conduct",
                                 "Social housing"))

ggplot(df, aes(x = qual_text, 
               y = meansat,
               fill = act_text)) +
  geom_col(position = position_dodge(width = 0.9), alpha = 0.7) +
  geom_errorbar(aes(ymin = losat, ymax = hisat),
                color = "black", width = 0.5,
                position = position_dodge(width = 0.9)) +
  facet_wrap(~ com_text, ncol = 3) +
  theme_bw(base_size = 14) +
  scale_color_grey(element_blank()) +
  scale_fill_grey(element_blank()) +
  xlab("") +
  ylab("Mean satisfaction") +
  theme(legend.position = "bottom")

ggsave("Figure_1.pdf", width = 12, height = 6, dpi = 333)


